<?php
class favorites{
	
	function add($file_id){
		global $dbs,$user ;
		$array['user_id'] = $user -> id();
		$array['file_id'] = $file_id;
		$array['created'] = date('Y-m-d H:i:s',time());
		
		$sql = 'SELECT * FROM `'. DB_PRE .'favorites` WHERE `user_id` = '. $user -> id() .' and `file_id` = '. $file_id;
		$lists = $dbs -> select($sql);
		if( count($lists) == 0 )
			$dbs -> insert(DB_PRE .'favorites',$array);
		exit('{"status":"ok","msg":"收藏成功"}');
	}
	
	
	function getList($user_id){
		global $dbs,$user,$page,$encrypt ;
		$sqlCount = 'SELECT * FROM `'. DB_PRE .'favorites` WHERE `user_id` = '.$user_id.' ORDER BY `id` DESC';
		$sql = $sqlCount .' limit '. (($page - 1) *  PAGE_SIZE ) .' , '. PAGE_SIZE;		
		$lists	= $dbs -> select($sqlCount);
		$total		= count($lists);
		
		$lists = $dbs -> select($sql);
		$html .='<table width="100%" border="0" cellspacing="1" cellpadding="3">'
			  .'<tr class="listTitle">'
				.'<th class="works">'.lang_works.'</th>'
				//.'<th class="name">名称</th>'	
				
				//.'<th class="time">时间</th>'		
				.'<th class="score">'.lang_grade.'</th>'
				//.'<th class="hits">点击</th>'	
				//.'<th class="comm">评论</th>'
							
				.'<th class="operate">'.lang_operate.'</th>'
			  .'</tr>';
		foreach($lists as $list){
			$sql = 'SELECT * FROM `'. DB_PRE .'file` WHERE `id` = '.$list['file_id'] ;
			$file= $dbs -> select($sql);
			$file = $file[0];
			if(count($file) == 0)
				continue ;
			$userInfo = $user -> getUser($file['user_id']);
			
			$html .='<tr>'
				.'<td class="thumbs">'
					.'<div class="itemthumb"><a class="beautyImg" target="_blank" title="'.htmlShow($list['name']).'" href="'.urlRewrite('option=media&id='.$encrypt -> code($list['file_id'])).'" target="_blank"><img src="'.self::getMediaThumb($list['file_id']).'" alt="'.htmlShow($file['name']).'" /></a></div>'
					.'<div class="itemdetail">'
						.'<h3 class="itemtitle">'
							.'<a title="'.htmlShow($list['name']).'" href="'.urlRewrite('option=media&id='.$encrypt -> code($list['file_id'])).'" target="_blank">'.htmlShow($file['name']).'</a></h3>'
						.'<p><span>'.lang_posted_by.'：<a href="'.urlRewrite('option=home&id='.$list['user_id']) .'">'. htmlShow($userInfo['nickName']) .'</a></span>'
							.'<span>'.lang_time.'：'.date('Y-m-d',strtotime($file['time'])).'</span></p>'
						.'<p><span>'.lang_hits.'：'.$file['hits'].'</span><span>'.lang_appraisal.'：'.$file['comments'].'</span></p>'
					.'</div>'
				.'</td>'
				//.'<td align=center>'.date('Y-m-d',strtotime($file['time'])).'</td>'
				.'<td align=center>'. self::creatScore((int)$file['star']) .'</td>'
				//.'<td align=center>'.$file['hits'].'</td>'
				//.'<td align=center>'.$file['comments'].'</td>'
				
				.'<td><span class="action view"><a href="'.urlRewrite('option=media&id='.$encrypt -> code($list['file_id'])).'" target="_blank">'.lang_view.'</a></span><span class="action del"><a href="'.WEB_ROOT_PATH.'?option=favoritesAction&type=action&do=del&id='. $encrypt -> code($list['id']) .'" class="del">'.lang_cancel_favorites.'</a></span></td>'
				.'<tr><td colspan="10" class="compart"><hr /></td></tr>';			
		}
		
		if( count($lists) == 0 )
			$html .= '<tr><td colspan="10" class="noWorks"><p class="emptylist">未收藏任何作品。</p></td></tr>';
		$html .= '</table>';
		
		if($isPage)
			$html .= getPages($page,$total,getPageFileUrl(),PAGE_SIZE, PAGE_LEN);
		if($isMore && $total > $num)
			$html .= '<h2 class="more"><a href="'.WEB_ROOT_PATH.'?option=taskList">'. lang_all .' &gt;</a></h2>';
		return $html;
	
	}
	
	function del($favorites_id){
		global $dbs,$user ;
		$dbs -> delete( DB_PRE .'favorites' , ' `id` = '. $favorites_id . ' AND `user_id` ='.$user -> id() );
		exit('{"status":"ok","msg":"删除成功"}');	
	}
	
	function creatScore($num){
		$html = '';
		$html = '<span class="star">';
		for($i = 1; $i <= 5 ; $i++){
			$class = $i <= $num ? ' class="a" ' : '';
			$html .= '<i'.$class.'></i>';
		}
		$html .= '</span>';
		return $html;
	}
	
	 
	
	function getMediaThumb(&$id){
		global $encrypt;
		if(!$id)
			return MEDIA_THUMB;
		return WEB_ROOT_PATH.'?option=getMediaAattachment&mediaType=file_thumb&id='.  $encrypt -> code($id);
	}
	
	function isFavorites($file_id){
		global $dbs,$user ;
		$sql = 'SELECT * FROM `'. DB_PRE .'favorites` WHERE `user_id` = '. $user -> id() .' and `file_id` = '. $file_id;
		$lists = $dbs -> select($sql);
		if( count($lists) == 0 )
			return false;
		return true;
	}
	

}

/*
class taskAssess{
	function getList( $task_id ,$invite , $timeEnd , $num  = 1 , $orderBy = 'id' ,$orderByType = 'DESC', $isPage = true , $isMore = false ){
		global $dbs,$page,$encrypt,$queryCode;
		$sqlCount = 'SELECT * FROM `'. DB_PRE .'task_attend` WHERE `task_id` = '.$task_id.' ORDER BY `'.$orderBy.'` '. $orderByType ;
		
		if(   ($page - 1) *  PAGE_SIZE  +   PAGE_SIZE  > $num )
			$sql = $sqlCount .' limit '. (  $num -  PAGE_SIZE < 0 ? 0 : $num -  PAGE_SIZE ) .' , '. $num ;
		else
			$sql = $sqlCount .' limit '. (($page - 1) *  PAGE_SIZE ) .' , '. PAGE_SIZE;;

		
		$ListCount	= $dbs -> select($sqlCount);
		$total		= count($ListCount);
		
		$lists = $dbs -> select($sql);
		$html .='<ul class="taskAssessList">';
		foreach($lists as $list){
			$file = $dbs -> select('SELECT * FROM `'.DB_PRE.'file` WHERE `id` = '.$list['file_id'] );
			$file = $file[0];
			$file_id = $encrypt->code($list['file_id']) ;
			$html .='<li>'
				.'<img src="'.self::getTaskThumb($file['thumb']).'" alt="'.htmlShow($file['name']).'" />'
				. '<h1>'.htmlShow($file['name']).'</h1>'
				.'<a class="view" target="_blank" title="'.htmlShow($file['name']).'" href="'.urlRewrite('option=media&id='.$file_id).'">'.lang_view.'&gt;&gt;</a>';				
				
				
				$sql = 'SELECT * FROM `'.DB_PRE.'star_assess` WHERE `task_id` = '.$task_id .' and `file_id` = '.$list['file_id'].' and `invite` = "'.$invite.'" and `timeEnd` = "'. $timeEnd .'" ';
				//echo $sql .'<br />';
				$stars = $dbs -> select($sql);
				$stars = $stars[0];
				//print_r($counts);
				if( count($stars) == 0 ){				
					$html .='<div class="star yellow">'.lang_grade.' : <span>';
					
				}else{
					$html .='<div class="star yellow">'.lang_grade.' : <span>';
				}
				for($i=1; $i<= 5; $i++){
					$class = $i <= (int)$stars['star'] ? ' class="a" ' : '';
					$html .='<a'.$class.' href="'.WEB_ROOT_PATH.'?option=taskAssessAction&type=action&do=star&star='.$i.'&id='.$file_id.'&query='.$queryCode.'"></a>';
				}
				
				
				$html .='<span></div>';
			$html .='</li>';                			
		}
		$html .= '<div class="clear"></div></ul>';
		
		if($isPage)
			$html .= getPages($page,$total,getPageFileUrl().'&query='.$queryCode,PAGE_SIZE, PAGE_LEN);
		if($isMore && $total > $num)
			$html .= '<h2 class="more"><a href="'.WEB_ROOT_PATH.'?option=taskList">'. lang_all .' &gt;</a></h2>';
		return $html;
		
	}
	
	function getTaskThumb($img){
		if(!$img)
			return TASK_THUMB;
		return $img;
	}
}*/
?>